#include "GlobalHead.h"
#include "CommandDraftTools.h"
#include "Gui_Application.h"
#include "MainWindow.h"
#include "Action.h"
#include "MDIView.h"
#include "View3DInventor.h"
#include "View3DInventorViewer.h"
#include "ToolsCallBack.h"
#include "part/Part2DObject.h"
#include "Gui_Document.h"
#include "CustomPlayer.h"


using namespace Gui;
static bool HasCB = false;

// ------------------------- [7/17/2012 Administrator]
ToolsCommand::ToolsCommand(const char* cname)
:Command(cname)
{

}
void ToolsCommand::deactivateTools(const char* toolName)const
{
	toolsManager* tm = toolsManager::Intance();
	Tools* tool =tm ->createTool(toolName);

	if(tool)
	{
		Tools* activetool = tm->findActiveTool();
		if(activetool)
			activetool->removeCallBack(activetool);
	}
}
void ToolsCommand::activateTools(const char* ToolsName, App::DocumentObject* cbObject )const
{
	toolsManager* tm = toolsManager::Intance();

	Tools* activetool = tm->findActiveTool();

	if(activetool)
	 activetool->removeCallBack(activetool);

	Tools* tool =tm ->createTool(ToolsName);
	if(tool)
	{
	   tool->pDocumentObject = cbObject;
		tool->addCallBack(tool);
	}
}

// --------------------------------- [7/17/2012 Administrator]

 void DraftTool::activated(const char* tn)
 {
 
 }
 bool DraftTool::isActive()
 {
 	return false;
 }
 
 void DraftTool::finish()
 {
 
 }

 ToolsCreator::ToolsCreator(DraftTool* dt)
	 :mdt(dt)
 {

 }
 void ToolsCreator::activated(const char* tname /* = "" */)
 {
    mdt->activated(tname);
 }
//////////////////////////////////////////////////////////////////////////

ToolsMove::ToolsMove()
:ToolsCommand("Draft_Move")//,mTm(tm)
{
 	sGroup        = QT_TR_NOOP("Drafting");
 	sMenuText     = QT_TR_NOOP("Move");
 	sToolTipText  = QT_TR_NOOP("Move the selected objects");
 	sWhatsThis    = "Draft_Move";
 	sStatusTip    = QT_TR_NOOP("Move object...");
 	sPixmap       = "Draft_Move";
 //	iAccel        = 0;

	//mTm = tm;

	//InitToolsCommandState();
}
ToolsMove::~ToolsMove()
{
   
}
void ToolsMove::activated(int iMsg)
{

	  App::DocumentObject *editObject = NULL;

	 Gui::Gui_Document *doc = Gui_Application::instance->activeDocument();
	if(doc)
	{
		if(doc->getInEdit() && doc->getInEdit()->isDerivedFrom(ViewProviderDocumentObject::getClassTypeId()))
		{
			ViewProviderDocumentObject* vp =static_cast<ViewProviderDocumentObject*>(doc->getInEdit());
			if(CCustomPlayer::Instance().isInActViewport(vp->getEntity()) == true)
				editObject =  vp->pcObject;
		}	
	
	}
	if(editObject == NULL)
		return;

   activateTools("movetool",editObject);
}
bool ToolsMove::isActive()
{
	return true;
}
void ToolsMove::Proceed()
{

}
void ToolsMove::finish()
{


}
void ToolsMove::move()
{

}
void ToolsMove::action()
{

}
void ToolsMove::numericInput()
{

}

//////////////////////////////////////////////////////////////////////////

ToolsRotate::ToolsRotate()
:ToolsCommand("Draft_Rotate")//,mTm(tm)
{
	sGroup        = QT_TR_NOOP("Drafting");
	sMenuText     = QT_TR_NOOP("Rotate");
	sToolTipText  = QT_TR_NOOP("Move the selected objects");
	sWhatsThis    = "Draft_Move";
	sStatusTip    = QT_TR_NOOP("Rotate object...");
	sPixmap       = "Draft_Rotate";
//	iAccel        = 0;

	//mTm = tm;
}

void ToolsRotate::activated(int iMsg)
{
	App::DocumentObject *editObject = NULL;

	Gui::Gui_Document *doc = Gui_Application::instance->activeDocument();
	if(doc)
	{
		if(doc->getInEdit() && doc->getInEdit()->isDerivedFrom(ViewProviderDocumentObject::getClassTypeId()))
		{
			ViewProviderDocumentObject* vp =static_cast<ViewProviderDocumentObject*>(doc->getInEdit());
			if(CCustomPlayer::Instance().isInActViewport(vp->getEntity()) == true)
				editObject =  vp->pcObject;
		}	

	}
	if(editObject == NULL)
		return;

	activateTools("rotatetool");

}
bool ToolsRotate::isActive()
{
	return true;
}

void ToolsRotate::finish()
{

}
void ToolsRotate::action()
{

}

void ToolsRotate::Proceed()
{

}

void ToolsRotate::numericInput()
{

}

ToolsScale::ToolsScale()
:ToolsCommand("Draft_Scale")//,mTm(tm)
{
	sGroup        = QT_TR_NOOP("Drafting");
	sMenuText     = QT_TR_NOOP("Scale");
	sToolTipText  = QT_TR_NOOP("Scale the selected objects");
	sWhatsThis    = "Draft_Move";
	sStatusTip    = QT_TR_NOOP("Scale object...");
	sPixmap       = "Draft_Scale";
	//iAccel        = 0;

	//mTm = tm;
}



void ToolsScale::activated(int iMsg)
{
	activateTools("scaletool");
}
bool ToolsScale::isActive()
{
   return true;
}

void ToolsScale::Proceed()
{

}
void ToolsScale::finish()
{

}
void ToolsScale::action()
{

}
void ToolsScale::numericInput()
{

}


ToolsLine::ToolsLine(/*ToolsCreator* tc*/)
:ToolsCommand("Draft_Line"),/*mtc(tc),*/isWire(false)
{
	sGroup        = QT_TR_NOOP("Drafting");
	sMenuText     = QT_TR_NOOP("Line");
	sToolTipText  = QT_TR_NOOP("Creates a 2-point line");
	sWhatsThis    = "Line";
	sStatusTip    = QT_TR_NOOP("Creates a 2-point line");
	sPixmap       = "Draft_Line";
}
ToolsLine::~ToolsLine()
{

}

void ToolsLine::activated(int iMsg)
{	

// 	mtc->activated("lineTool");
//    pObject = NULL;
// 	pObject = GetApp_Application().getActiveDocument()->addObject("Part::Feature","Line");
	pObject = GetApp_Application().getActiveDocument()->addObject("Part::Part2DObject","Line");
	pObject->setAsImport();
	Command::doCommand((CommandStringFunction)App::Document::recomputeEntity);
	pObject->finshImport();
	activateTools("lineTool",pObject);


// 	Tools* lt = toolsManager::Intance()->createTool("lineTool");
//     lt->addCallBack()
}
void ToolsLine::action()
{

}
/************************************************************************/
/* toolsWire                                                             */
/************************************************************************/
ToolsWire::ToolsWire()
:ToolsCommand("Draft_Wire")
{
	sGroup        = QT_TR_NOOP("Drafting");
	sMenuText     = QT_TR_NOOP("DWire");
	sToolTipText  = QT_TR_NOOP("Creates a 2-point line");
	sWhatsThis    = "Line";
	sStatusTip    = QT_TR_NOOP("Creates a multiple-point DraftWire (DWire)");
	sPixmap       = "Draft_Wire";
}
void ToolsWire::activated(int iMsg)
{
	App::DocumentObject* pObject = GetApp_Application().getActiveDocument()->addObject("Part::Feature","wire");
	activateTools("wireTool");
}

/************************************************************************/
/* toolsBSpline                                                         */
/************************************************************************/

ToolsBSline::ToolsBSline()
:ToolsCommand("Draft_BSpline")
{
	sGroup        = QT_TR_NOOP("Drafting");
	sMenuText     = QT_TR_NOOP("DWire");
	sToolTipText  = QT_TR_NOOP("Creates a 2-point line");
	sWhatsThis    = "B-Spline";
	sStatusTip    = QT_TR_NOOP("Creates a multiple-point b-spline");
	sPixmap       = "Draft_BSpline";
}
void ToolsBSline::activated(int iMsg)
{
	App::DocumentObject* pObject = GetApp_Application().getActiveDocument()->addObject("Part::Feature","BSpline");
	activateTools("BSplineTool");
}

/************************************************************************/
/* toolsArc                                                         */
/************************************************************************/
ToolsArc::ToolsArc()
:ToolsCommand("Draft_Arc")
{
	sGroup        = QT_TR_NOOP("Drafting");
	sMenuText     = QT_TR_NOOP("DWire");
	sToolTipText  = QT_TR_NOOP("Creates an arc");
	sWhatsThis    = "Arc";
	sStatusTip    = QT_TR_NOOP("Creates an arc");
	sPixmap       = "Draft_Arc";
}
void ToolsArc::activated(int iMsg)
{
	App::DocumentObject* pObject = GetApp_Application().getActiveDocument()->addObject("Part::Feature","BSpline");
	activateTools("arcTools");
}
/************************************************************************/
/* toolsCircle                                                           */
/************************************************************************/
ToolsCircle::ToolsCircle()
:ToolsCommand("Draft_Circle")
{
	sGroup        = QT_TR_NOOP("Drafting");
	sMenuText     = QT_TR_NOOP("Circle");
	sToolTipText  = QT_TR_NOOP("Creates a 2-point line");
	sWhatsThis    = "Circle";
	sStatusTip    = QT_TR_NOOP("Creates a circle");
	sPixmap       = "Draft_Circle";
}
void ToolsCircle::activated(int iMsg)
{
	//App::DocumentObject* pObject = GetApp_Application().getActiveDocument()->addObject("Part::Feature","Circle");
	App::DocumentObject* pObject = GetApp_Application().getActiveDocument()->addObject("Part::Part2DObject","Circle");
	pObject->setAsImport();
	Command::doCommand((CommandStringFunction)App::Document::recomputeEntity);
	pObject->finshImport();
	activateTools("CircleTool",pObject);
}

/************************************************************************/
/* toolsDimension                                                       */
/************************************************************************/
ToolsDimension::ToolsDimension()
:ToolsCommand("Draft_Dimentsion")
{

}
void ToolsDimension::activated(int iMsg)
{
	App::DocumentObject* pObject = GetApp_Application().getActiveDocument()->addObject("Part::Feature","Dimentsion");
	activateTools("DimentsionTools");
}


void CreateToolsCommands()
{
// 	DraftTool* dt = new DraftTool;
// 	ToolsCreator* tc = new ToolsCreator(dt);

	CommandManager &rcCmdMgr = Gui_Application::instance->commandManager();

	rcCmdMgr.addCommand(new ToolsMove());
	rcCmdMgr.addCommand(new ToolsRotate());
	rcCmdMgr.addCommand(new ToolsScale());

	rcCmdMgr.addCommand(new ToolsLine());
	rcCmdMgr.addCommand(new ToolsCircle());
}








